package com.sunwayworld.basemodule.business.result.service.impl;

import com.sunwayworld.framework.mybatis.mapper.MapperParameter;
import com.sunwayworld.framework.support.PersistableHelper;
import com.sunwayworld.framework.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.business.result.dao.LimsRunMaterialDao;
import com.sunwayworld.basemodule.business.result.bean.LimsRunMaterialBean;
import com.sunwayworld.basemodule.business.result.service.LimsRunMaterialService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsRunMaterialServiceImpl implements LimsRunMaterialService {

    @Autowired
    private LimsRunMaterialDao limsRunMaterialDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsRunMaterialDao getDao() {
        return limsRunMaterialDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsRunMaterialBean limsRunMaterial = jsonWrapper.parseUnique(LimsRunMaterialBean.class);
        limsRunMaterial.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsRunMaterial);
        return limsRunMaterial.getId();
    }

    @Override
    public List<LimsRunMaterialBean> selectMaterialInfoByRunId(MapperParameter parameter) {
        List<Map<String, Object>> maps = getDao().selectMaterialInfoByRunId(parameter);
        return maps.isEmpty() ? CollectionUtils.emptyList() :
                maps.parallelStream().map(e -> PersistableHelper.mapToPersistable(e, LimsRunMaterialBean.class)).collect(Collectors.toList());
    }
}
